package com.igl.talk.android.client.ide.activities;

import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.igl.talk.android.client.R;
import com.igl.talk.android.client.application.BaseApplication;
import com.igl.talk.android.client.core.handlers.ClientHandler;
import com.igl.talk.android.client.core.service.Server;
import com.igl.talk.android.client.ide.models.IdeMessageModel;
import com.igl.talk.android.client.ide.models.MessageAdapter;
import com.igl.talk.android.client.ide.dialogs.DialogsFactory;

public class MainViewActivity extends BaseServiceBindingActivity {

    private static final String MESSAGE_KEY = "com.igl.talk.android.client.MESSAGE";

    private ListView messagesView;
    private MessageAdapter adapter;
    private EditText inputText;

    private String username;

    private final ClientHandler handler = ClientHandler.getInstance();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainview);

        messagesView = (ListView) findViewById(R.id.messageView);

        adapter = new MessageAdapter(getApplicationContext(), R.layout.message);

        handler.setViewAdapter(adapter);
        handler.setApplicationContext(getApplicationContext());

        messagesView.setAdapter(adapter);

        inputText = (EditText) findViewById(R.id.messageInputField);

        // getting username from RegisterActivity.class
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            username = bundle.getString(RegisterActivity.USERNAME, "sender");
            handler.setClientUsername(username);
        }
    }

    private void addItems() {
        adapter.add(new IdeMessageModel(username, username, false));

        for (int i = 0; i < 20; i++) {
            adapter.add(new IdeMessageModel("testuser", "test message", true));
        }
    }

    public void sendMessage(View view) {
        IdeMessageModel message = new IdeMessageModel(username, inputText.getText().toString(), false);
        Server.INSTANCE.sendMessage(message);
        adapter.add(message);
        inputText.setText("");
    }

    @Override
    protected void onDestroy() {
        Server.INSTANCE.logout();
        Server.INSTANCE.closeConnection();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (!preferences.getBoolean(BaseApplication.PREF_EXIT_WITHOUT_ASKING, false)) {
            Dialog dialog = DialogsFactory.EXIT_DIALOG.getDialog(MainViewActivity.this);
            dialog.show();
        } else {
            super.exit();
        }
    }

    @Override
    protected void onResume() {
        final NotificationManager notificationManager = (NotificationManager) getApplicationContext().
                getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        super.onResume();
    }
}
